
#ifndef __DB_ARBITRATOR_H
#define __DB_ARBITRATOR_H

#include <apr-1/apr_pools.h>
#include "KeyDB.h"
#include "PendingDB.h"
#include "ActorsDB.h"
#include "ClusterSSHDB.h"

#define CHECKSTATE_DOES_NOT_EXIST       0
#define CHECKSTATE_RESERVED             1
#define CHECKSTATE_EXISTS_UNAUTHORISED  2

#define PROCESSXMLDESCRIPTOR_SUCCESS       0
#define PROCESSXMLDESCRIPTOR_BADXML        1
#define PROCESSXMLDESCRIPTOR_DUPLICATE     2
#define PROCESSXMLDESCRIPTOR_BLOCKTOOBIG   3
#define PROCESSXMLDESCRIPTOR_COULDNOTWRITE 4
#define PROCESSXMLDESCRIPTOR_OUTOFQUOTA    5
#define PROCESSXMLDESCRIPTOR_GENERICERROR  6

class AllDBs
{
   public:
      void SetAllNull()
      {
         keydb = NULL;
         pendingdb = NULL;
         actorsdb = NULL;
         sshkeydb = NULL;
         clustersdb = NULL;
      }
      void DeleteAll()
      {
         if (keydb) delete keydb;
         if (pendingdb) delete pendingdb;
         if (actorsdb) delete actorsdb;
         if (sshkeydb) delete sshkeydb;
         if (clustersdb) delete clustersdb;
      }
      void SetKeyDB(KeyDB *ikeydb) { keydb = ikeydb; }
      void SetSSHKeyDB(KeyDB *isshkeydb) { sshkeydb = isshkeydb; }
      void SetClusterSSHDB(ClusterSSHDB *iclustersshdb) { clustersdb = iclustersshdb; }
      void SetPendingDB(PendingDB *ipendingdb) { pendingdb = ipendingdb; }
      void SetActorsDB(ActorsDB *iactorsdb) { actorsdb = iactorsdb; }
      AllDBs() { SetAllNull(); }
      ~AllDBs() { DeleteAll(); SetAllNull(); }
      KeyDB *keydb;
      KeyDB *sshkeydb;
      ClusterSSHDB *clustersdb;
      PendingDB *pendingdb;
      ActorsDB *actorsdb;
};

class DBArbitrator
{
   public:
      static int CheckForUser(AllDBs *dbs, const char *name);
      static int UserAddRequestAccept(AllDBs *dbs, const char *name, const char *date, const char *publickey);
      static int DeleteFileStoreByUUID(AllDBs *dbs, const char *uuid, apr_pool_t *mp);
      static void UnuseAndDeleteAllKeysDependentOnUUID(AllDBs *dbs, const char *uuid);
      static void CleanExpiredComputeSessions(AllDBs *dbs);
      static char *LookupComputeUltimateUser(AllDBs *dbs, const char *uuid);
      static void ScheduleComputeResources(int instances, const char *clusterid, const char *password, apr_pool_t *imp);
};

#endif
